#!/usr/bin/python
#-*- coding: utf-8 -*-

import sys, cv, cv2
import numpy as np

pic1, pic2, pic3 = 'cover_left.JPG', 'cover_right.JPG', 'cover_top.JPG'
pics = [pic1, pic2, pic3]
images = None

def read_images():
    # Bilder aus Datei lesen
    image1 = cv2.imread(pics[0], cv2.CV_LOAD_IMAGE_COLOR)
    image2 = cv2.imread(pics[1], cv2.CV_LOAD_IMAGE_COLOR)
    image3 = cv2.imread(pics[2], cv2.CV_LOAD_IMAGE_COLOR)
    return [image1, image2, image3]

def show(img, # numpyarray
         windowname # unique string
         ):
    cv2.namedWindow(windowname, cv2.CV_WINDOW_AUTOSIZE)
    cv2.imshow(windowname, img)
    
def show_pics(lis, # liste von stringnamen         
         ):
    # Window erzeugen und Bild darin anzeigen
    for n in range(len(lis)):
        name = pics[n]
        cv2.namedWindow(name, cv2.CV_WINDOW_AUTOSIZE)
        cv2.imshow(name, images[n])

    
def luminance (left, top, right):
    return cv2.add(left, top, right) / 3

def p(l, r):
    print '-------------'
    print 'left', l
    print
    print 'right', r
    print '-------------'
    print
    
def bias (left, right):
    p(left,right)
    left, right = np.divide(left,2), np.divide(right,2)
    p(left, right)
    
    temp1 = np.subtract(left, right)
    print
    print 'temp1', temp1
    temp2 = np.add(temp1,127)
    print
    print 'temp2', temp2
    return temp2

def close(lis):
    for win in lis:
        print 'schliesse', win
        cv2.destroyWindow(win)
        
        
def make_gray (image):
    """Konvertiert image von RGB zu Graustufenbild"""
    return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    
if __name__ == '__main__':


    # Bilder einlesen
##    global images
##    images = read_images()

    # Bilder anzeigen
##    show_pics(pics)

    r = cv2.imread('cover_right.JPG', cv2.CV_LOAD_IMAGE_GRAYSCALE)
    print r.tolist()[0]
    
    r_gray = cv2.cvtColor(r, cv2.COLOR_BGR2GRAY)
    
    # Bild in Graustufen
##    grays = map(make_gray, images)
##    left, right, top = grays[0], grays[1], grays[2]
##    print right.tolist()[0]
    
##    show(left, 'left')
##    show(top, 'top')
##    show(right, 'right')

##    print left.tolist()[0]
        
    # luminance-Bild berrechnen
##    i_luminance = luminance(left, top, right)
##    show(i_luminance, 'luminance')
   
    
##    left, right = np.arange(1.,4.), np.arange(2.,5.)
    
##    i_bias = bias(left, right)
    
##    show(i_bias, 'bias')
    
    cv2.waitKey(0) # unendlich warten auf Tastendruck
##    close(['left','top','right','luminance']+pics)
    cv2.destroyAllWindows()
    

        
    
